/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogImage.java
 *
 * Created on 12-may-2012, 10:54:27
 */
package com.ufps.cuatro.vistas.representacion;
 
import java.io.File;
import java.util.Observer;

import javax.swing.JFileChooser;

import org.jdesktop.application.Action;

import com.ufps.cuatro.CuatroApp;
import com.ufps.cuatro.controlador.render.UtilRender;
import com.ufps.cuatro.modelo.representacion.componente.Componente;
import com.ufps.cuatro.util.Bandera;
import com.ufps.cuatro.util.MessageWindow;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.vistas.layout.JPanelExplorerTreeView;

/**
 *
 * @author Catc
 */
public class JDialogImage extends javax.swing.JDialog implements MessageWindow{
    private static final long serialVersionUID = 4620237990680611593L;
    
    private File objFile;
    private Object objParent;
    private Componente objComponente;

    /** Creates new form JDialogImage */
    public JDialogImage(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setIconImage(Utileria.leerIconoAppEnImagen(Utileria.MODO_16x16).getImage());
    }

    public JDialogImage(Object objParent, Componente objComponente) {
        super(CuatroApp.getApplication().getMainFrame(), true);
        initComponents();
        this.setIconImage(Utileria.leerIconoAppEnImagen(Utileria.MODO_16x16).getImage());
        this.objParent = objParent;
        this.objComponente = objComponente;
    }
    
    public JDialogImage(JPanelExplorerTreeView objParent, Componente objComponente) {
        super(CuatroApp.getApplication().getMainFrame(), true);
        initComponents();
        this.setIconImage(Utileria.leerIconoAppEnImagen(Utileria.MODO_16x16).getImage());
        this.objParent = objParent;
        this.objComponente = objComponente;
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jLabelImg = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton3 = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabelPath = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jLabelImg.setForeground(new java.awt.Color(153, 153, 153));
        jLabelImg.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelImg.setText("Sin Imagen");
        jLabelImg.setName("jLabelImg"); // NOI18N
        jScrollPane1.setViewportView(jLabelImg);

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ufps/cuatro/resources/icono app x 32.png"))); // NOI18N
        jLabel2.setText("<html><p><b>Incluir Imagen:</b></p><p>Seleccione la Imagen que desea Agregar a la Representación.</p>");
        jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setOpaque(true);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(JDialogImage.class, this);
        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        jButton2.setAction(actionMap.get("incluirImg")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jButton3.setAction(actionMap.get("cancelar")); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar1.add(jSeparator3);

        jButton4.setAction(actionMap.get("help")); // NOI18N
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        jLabel1.setText("Ubicación:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabelPath.setBackground(new java.awt.Color(255, 255, 255));
        jLabelPath.setText("Sin Incluir");
        jLabelPath.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelPath.setName("jLabelPath"); // NOI18N
        jLabelPath.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelPath, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelPath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(8, 8, 8)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDialogImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDialogImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDialogImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDialogImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JDialogImage dialog = new JDialogImage(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void aceptar() {
        if(objFile == null){
            msgWarning("Debe Seleccionar un Archivo de Imagen.");
            return;
        }
        if(msgConfirm("¿Acepta la Imagen Seleccionada?")){
            Bandera objBandera = new Bandera();
            objBandera.addObserver((Observer) objParent);
            objBandera.setEstado(Bandera.RECURSO_AGREGADO);                
            objBandera.setFlag(true);
            objComponente.setObjConfig(objFile);
            objBandera.setObj(objFile);
            objBandera.notificar();      
            dispose();
        }
    }

    @Action
    public void incluirImg() {
        javax.swing.JFileChooser fc = new JFileChooser();
        javax.swing.filechooser.FileNameExtensionFilter filter =
        new javax.swing.filechooser.FileNameExtensionFilter(
        "Archivo de Imagen",
        "jpg", "jpeg", "png", "gif", "bmp");

        fc.setFileFilter(filter);        

        File fileDir = null;       

        if (Utileria.strTempDir.compareTo("") == 0) {
            fc.changeToParentDirectory();
        } 
        else {
            fileDir = new File(Utileria.strTempDir);
            fc.setCurrentDirectory(fileDir);
        }

        int option = fc.showDialog(fc, "Seleccione un Archivo de Imagen");

        if (JFileChooser.APPROVE_OPTION == option) {
            Utileria.strTempDir = fc.getSelectedFile().getParent();
            if(fc.getSelectedFile().exists()){
                objFile = fc.getSelectedFile();
                UtilRender.loadIconoJComponent(jLabelImg, objFile.getAbsolutePath());
                jLabelPath.setText(objFile.getAbsolutePath());
            }
        }
    }

    @Action
    public void cancelar() {
        if(msgConfirm("¿Desea Cancelar el Proceso?")){
            Bandera objBandera = new Bandera();
            objBandera.addObserver((Observer) objParent);
            objBandera.setEstado(Bandera.PROCESO_CANCELADO);                
            objBandera.setFlag(false);
            objBandera.setObj(null);
            objBandera.notificar(); 
            dispose();
        }
    }

    @Action
    public void help() {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelImg;
    private javax.swing.JLabel jLabelPath;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    public boolean msgConfirm(String strMsg) {
        if(objParent instanceof JPanelExplorerTreeView){
            JPanelExplorerTreeView objPanel = (JPanelExplorerTreeView) objParent;
            return UtilRender.msgConfirmar(strMsg, objPanel);            
        }
        else{
            return UtilRender.msgConfirmar(strMsg, this);            
        }        
    }

    public void msgInfo(String strMsg) {
        UtilRender.msgInformacion(strMsg, this);                    
    }

    public void msgWarning(String strMsg) {        
        UtilRender.msgAdvertencia(strMsg, this);                    
    }

    public void msgError(String strMsg) {        
        UtilRender.msgError(strMsg, this);                    
    }

    
}
